package com.easyidea.its.web.action.carseizure;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.easyidea.its.domain.model.passport.TrackLog;
import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.facade.tracklog.TrackLogMgr;
import com.easyidea.its.util.DateUtil;
import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.util.PageUtil;
import com.easyidea.its.util.Result;
import com.easyidea.its.web.action.base.ItsBaseAction;
import com.easyidea.its.web.dto.CarSeizureDto;
import com.easyidea.its.web.dto.SeesionUserDto;
import com.easyidea.its.web.dto.TrackLogExport;

public class CarSeizureAction extends ItsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5756321346120367356L;
	
	private TrackLogMgr trackLogMgr;
	
	private Result<TrackLog> result;
	
	private Integer totalNum;

	private CarSeizureDto carSeizureDto;
	
	private Collection<Department> allDepartments;
	
	private Collection<PassPort> passPorts;
	
	private Collection<String> directions;
	
	private String goBack;
	
	private List<TrackLogExport> dataset;
	
	public Collection<Department> getAllDepartments() {
		return allDepartments;
	}

	public void setAllDepartments(Collection<Department> allDepartments) {
		this.allDepartments = allDepartments;
	}
	/**
	 * 精确追踪
	 * @return
	 */
	public String initStrictfpSeizureList() {
		initCarSeizureDto();
		this.allDepartments = trackLogMgr.getAllDepartments();
		return SUCCESS;
	}

	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	private void initCarSeizureDto() {
		if(carSeizureDto == null) {
			carSeizureDto = new CarSeizureDto();
		}
		Date currentDate =new Date();
		carSeizureDto.setStartDate(currentDate);
		carSeizureDto.setEndDate(currentDate);
		carSeizureDto.setEndHour(currentDate.getHours());
		carSeizureDto.setEndMinute(currentDate.getMinutes());
		carSeizureDto.setPlateNumber("*");
	}
	/**
	 * 模糊追踪
	 * @return
	 */
	public String fuzzySearch() {
		initCarSeizureDto();
		this.allDepartments = trackLogMgr.getAllDepartments();
		passPorts = trackLogMgr.getAllPassPorts();
		return SUCCESS;
	}	
	@SuppressWarnings("unchecked")
	public String strictfpSeizureList() {
		this.allDepartments = trackLogMgr.getAllDepartments();
		SeesionUserDto dto = (SeesionUserDto)getSession().getAttribute(ItsConstants.SESSION_KEY_USER_INFO);
			
		if (goBack != null && goBack.equals("true")) {
		 if(getSession().getAttribute(dto.getUserId()+"."+ ItsConstants.SESSION_KEY_CAR_SEIZURE) != null) {
			 carSeizureDto = (CarSeizureDto)getSession().getAttribute(dto.getUserId()+"."+ ItsConstants.SESSION_KEY_CAR_SEIZURE); 
		 }
			
		}
		if(carSeizureDto != null  && carSeizureDto.getDepartmentId() != null && !carSeizureDto.getDepartmentId().equals("all")) {
			passPorts = this.trackLogMgr.getPassPortsByDeptId(carSeizureDto.getDepartmentId());
				directions = new ArrayList<String>();  
				for (PassPort p : passPorts) {
					if(p.getPassPortName().equals(carSeizureDto.getPassportName())){
						directions.add(p.getStartpoint());
						directions.add(p.getEndpoint());
						break;
					}
			}
		}
		getSession().removeAttribute(dto.getUserId()+"."+ItsConstants.SESSION_KEY_TRACKLOG_INFO);
		totalNum = trackLogMgr.getTrackLogsCountByCondition(carSeizureDto);
		if (totalNum > 0) {
			List<TrackLog> records = trackLogMgr.getTrackLogsByCondition(carSeizureDto, (currentPage - 1) * pageSize,
					pageSize);					
			result = PageUtil.createResult(pageSize, currentPage, totalNum, records);
		} else {
			result = PageUtil.createResult(pageSize, currentPage, 0, new ArrayList());
		}
		getSession().setAttribute(dto.getUserId()+"."+ ItsConstants.SESSION_KEY_CAR_SEIZURE,carSeizureDto);	
		return carSeizureDto.getSearchType();
	}
	public String exportTrackLog() {
		SeesionUserDto dto = (SeesionUserDto) getSession()
		.getAttribute(ItsConstants.SESSION_KEY_USER_INFO);
		dataset = new ArrayList<TrackLogExport>();
		if (getSession().getAttribute(
				dto.getUserId() + "."
						+ ItsConstants.SESSION_KEY_CAR_SEIZURE) != null) {
			CarSeizureDto carSeizureDto = (CarSeizureDto)getSession().getAttribute(
							dto.getUserId() + "."
									+ ItsConstants.SESSION_KEY_CAR_SEIZURE);
			List<TrackLog> records = trackLogMgr.getTrackLogsByCondition(
					carSeizureDto, 0, 0);
			for (TrackLog trackLog : records) {
				dataset.add(new TrackLogExport(trackLog.getPlateNumber(),
						trackLog.getPlateType(), trackLog.getPassTime(),
						trackLog.getPassPort().getPassPortName(), trackLog
								.getLaneName(), String.valueOf(trackLog.getSpeed())+ " km/h",
						trackLog.getDirection()));
			}
		}
		return SUCCESS;
	}
	
	public List<TrackLogExport> getDataset() {
		return dataset;
	}

	public void setDataset(List<TrackLogExport> dataset) {
		this.dataset = dataset;
	}

	public Collection<String> getDirections() {
		return directions;
	}

	public void setDirections(Collection<String> directions) {
		this.directions = directions;
	}

	public CarSeizureDto getCarSeizureDto() {
		return carSeizureDto;
	}

	public void setCarSeizureDto(CarSeizureDto carSeizureDto) {
		this.carSeizureDto = carSeizureDto;
	}
	public void setTrackLogMgr(TrackLogMgr trackLogMgr) {
		this.trackLogMgr = trackLogMgr;
	}

	public Result<TrackLog> getResult() {
		return result;
	}

	public void setResult(Result<TrackLog> result) {
		this.result = result;
	}

	public TrackLogMgr getTrackLogMgr() {
		return trackLogMgr;
	}

	public Integer getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(Integer totalNum) {
		this.totalNum = totalNum;
	}

	public String getGoBack() {
		return goBack;
	}

	public void setGoBack(String goBack) {
		this.goBack = goBack;
	}

	public Collection<PassPort> getPassPorts() {
		return passPorts;
	}

	public void setPassPorts(Collection<PassPort> passPorts) {
		this.passPorts = passPorts;
	}
	
}
